package com.Mobile.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.Entity.Userdata;
import com.Entity.Userhealthdata;
import com.Entity.Users;
import com.Service.UserHealthDataService;
import com.Service.UserdataService;
import com.Service.UsersService;

public class MobileSaveHealthDataAction implements ServletResponseAware{
//存储前台传过来的孕妇的舒张压、收缩压、血糖、体重和尿蛋白
	private HttpServletResponse response;//response闁诲海鏁搁、濠囨寘?
	private int flag=0;
	@Autowired
	private UsersService usersservice;
	@Autowired
	private UserHealthDataService userhealthdataservice;
	@Autowired
	private UserdataService userdataservice;
	private Users user=new Users();
	private Userhealthdata userhealthdata=new Userhealthdata();
	private Userdata userdata=new Userdata();
	
	
	public void savehealthdata() throws ServletException,IOException{
		// TODO Auto-generated method stub
		String userid=ServletActionContext.getRequest().getParameter("UserID");
		String username=ServletActionContext.getRequest().getParameter("UserName");
		String diastolicpressure=ServletActionContext.getRequest().getParameter("DiastolicPressure");
		String systolicpressure=ServletActionContext.getRequest().getParameter("SystolicPressure");
		String bloodsugar=ServletActionContext.getRequest().getParameter("BloodSugar");
		String weight=ServletActionContext.getRequest().getParameter("Weight");
		String uriaprotein=ServletActionContext.getRequest().getParameter("UriaProtein");
		//防止传上来的username是中文，会出现乱码（google浏览器测试出现）
		username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
			
		System.out.println(username);		
		userid = java.net.URLDecoder.decode(userid,"UTF-8");
		username = java.net.URLDecoder.decode(username,"UTF-8");
		diastolicpressure = java.net.URLDecoder.decode(diastolicpressure,"UTF-8");
		systolicpressure = java.net.URLDecoder.decode(systolicpressure,"UTF-8");
		bloodsugar = java.net.URLDecoder.decode(bloodsugar,"UTF-8");
		weight = java.net.URLDecoder.decode(weight,"UTF-8");
		uriaprotein = java.net.URLDecoder.decode(uriaprotein,"UTF-8");
		
		//转化成float
		/*float f=Float.valueOf(s);
        float f1=new Float(s);*/
		float diastolicpressure1=Float.valueOf(diastolicpressure);
		float systolicpressure1=Float.valueOf(systolicpressure);
		float bloodsugar1=Float.valueOf(bloodsugar);
		float weight1=Float.valueOf(weight);
		float uriaprotein1=Float.valueOf(uriaprotein);
		
		JSONObject returnjson=new JSONObject();
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8"); 
		try {
			PrintWriter out = response.getWriter();
			int id=Integer.parseInt(userid);
			user=this.usersservice.getById(id);
			String trueusername=user.getUserName();
			String realname=this.userdataservice.getById(id).getRealName();
			System.out.println(trueusername);
		    if(username.equals(trueusername)){
		    	flag=1;
		    	userhealthdata.setUserId(id);
		    	userhealthdata.setDiastolicPressure(diastolicpressure1);
		    	userhealthdata.setSystolicPressure(systolicpressure1);
		    	userhealthdata.setBloodSugar(bloodsugar1);
		    	userhealthdata.setWeight(weight1);
		    	userhealthdata.setUriaProtein(uriaprotein1);
		    	userhealthdata.setDataUploadTime(new Date());
		    	userhealthdata.setRealName(realname);
		    	this.userhealthdataservice.save(userhealthdata);
		    	returnjson.put("flag", flag);
		    }else{
				flag=0;
				returnjson.put("flag", flag);
			}						
			out.println(returnjson.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public Userhealthdata getUserhealthdata() {
		return userhealthdata;
	}

	public void setUserhealthdata(Userhealthdata userhealthdata) {
		this.userhealthdata = userhealthdata;
	}

	public Userdata getUserdata() {
		return userdata;
	}

	public void setUserdata(Userdata userdata) {
		this.userdata = userdata;
	}

}
